package com.hqyj.springboot.task;

import com.hqyj.springboot.mapper.ScheduledMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ScheduledTask implements SchedulingConfigurer {

    @Resource
    protected ScheduledMapper scheduledMapper;

    //控制台每隔6秒钟会输出一次"hello"

    //@Scheduled(cron ="*/6 * * * * ?")
    /*
    public void sayHello() {
        System.out.println("hello");
    }*/

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(() -> process(),
                triggerContext -> {
                    String cron = scheduledMapper.getCron(1);
                    if (cron.isEmpty()) {
                        System.out.println("cron is null");
                    }
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                });
    }

    //这里写定时任务要做的事情...
    private void process() {
        System.out.println("hello,world!");
    }



}
